﻿using AutomatedTestDataManagement.Models;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;

namespace AutomatedTestDataManagement.Controllers
{
    public class HTMLFieldController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /HTMLField/

        public ActionResult Index()
        {
            var htmlfields = db.HTMLFields.Include(h => h.FieldType);
            return View(htmlfields.ToList());
        }

        //
        // GET: /HTMLField/Details/5

        public ActionResult Details(int id = 0)
        {
            HTMLField htmlfield = db.HTMLFields.Find(id);
            if (htmlfield == null)
            {
                return HttpNotFound();
            }
            return View(htmlfield);
        }

        //
        // GET: /HTMLField/Create
        /// <summary>
        /// Prepares for field creation view. Optionally sets parent site ID and parent page ID
        /// </summary>
        /// <param name="id">parent page ID</param>
        /// <param name="siteID">parent site ID</param>
        /// <returns>View for creating new field</returns>
        public ActionResult Create(int id = 0, int siteID = 0)
        {
            ViewBag.FieldTypeID = new SelectList(db.FieldTypes, "FieldTypeID", "FieldTypeName");
            if (id != 0)
            {
                ViewBag.PageID = id;
            }
            if (siteID != 0)
            {
                ViewBag.SiteID = siteID;
            }
            return View();
        }

        //
        // POST: /HTMLField/Create
        /// <summary>
        /// Responds to HTTPPOST action only. If parent pageID is specified, adds the new field to the parent page
        /// </summary>
        /// <param name="htmlfield">field object</param>
        /// <returns>if page ID is specified, returns to the page edit view, with any site ID information that exists. Otherwise returns to the field index view</returns>
        [HttpPost]
        public ActionResult Create(HTMLField htmlfield)
        {
            var pageID = Request.Form["PageID"];
            var siteID = Request.Form["SiteID"];
            
            if (ModelState.IsValid)
            {
                db.HTMLFields.Add(htmlfield);
                db.SaveChanges();

                if (pageID != null)
                {
                    // Add the field to the page
                    PagesField pagesfield = new PagesField();
                    pagesfield.PageID = System.Convert.ToInt32(pageID);
                    pagesfield.FieldID = htmlfield.FieldID;
                    TryUpdateModel(pagesfield, new string[] { "PageID", "FieldID" });
                    if (ModelState.IsValid)
                    {
                        db.PagesFields.Add(pagesfield);
                        db.SaveChanges();
                        if (siteID != null)
                        {
                            return RedirectToAction("Edit", "Page", new { id = pageID, siteID = siteID });
                        }
                        else
                        {
                            return RedirectToAction("Edit", "Page", new { id = pageID });
                        }
                    }
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }

            if (pageID != null) { ViewBag.PageID = pageID; }
            if (siteID != null) { ViewBag.SiteID = siteID; }
            ViewBag.FieldTypeID = new SelectList(db.FieldTypes, "FieldTypeID", "FieldTypeName", htmlfield.FieldTypeID);
            return View(htmlfield);
        }

        //
        // GET: /HTMLField/Edit/5
        /// <summary>
        /// Edits a given field. If pageID or siteID are specified, adds them to the view bag for use when the post is processed
        /// </summary>
        /// <param name="id">field ID</param>
        /// <param name="pageID">parent page ID</param>
        /// <param name="siteID">parent site ID</param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0, int pageID = 0, int siteID = 0)
        {
            
            HTMLField htmlfield = db.HTMLFields.Find(id);
            if (htmlfield == null)
            {
                return HttpNotFound();
            }
            if (pageID != 0) { ViewBag.PageID = pageID; }
            if (siteID != 0) { ViewBag.SiteID = siteID; }
            ViewBag.FieldTypeID = new SelectList(db.FieldTypes, "FieldTypeID", "FieldTypeName", htmlfield.FieldTypeID);
            return View(htmlfield);
        }

        //
        // POST: /HTMLField/Edit/5
        /// <summary>
        /// Responds to HTTPPOST action only. Updates given field data
        /// </summary>
        /// <param name="htmlfield">field to update</param>
        /// <returns>if parent pageID is present, return to the edit view of the page, otherwise returns to the field index view. SiteID is passed through if present</returns>
        [HttpPost]
        public ActionResult Edit(HTMLField htmlfield)
        {
            var pageID = Request.Form["PageID"];
            var siteID = Request.Form["SiteID"];

            if (ModelState.IsValid)
            {
                db.Entry(htmlfield).State = EntityState.Modified;
                db.SaveChanges();

                if (pageID != null)
                {
                    if (siteID != null)
                    {
                        return RedirectToAction("Edit", "Page", new { id = pageID, siteID = siteID });
                    }
                    else
                    {
                        return RedirectToAction("Edit", "Page", new { id = pageID });
                    }
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            if (pageID != null) { ViewBag.PageID = pageID; }
            if (siteID != null) { ViewBag.SiteID = siteID; }
            ViewBag.FieldTypeID = new SelectList(db.FieldTypes, "FieldTypeID", "FieldTypeName", htmlfield.FieldTypeID);
            return View(htmlfield);
        }

        //
        // GET: /HTMLField/Delete/5

        public ActionResult Delete(int id = 0)
        {
            HTMLField htmlfield = db.HTMLFields.Find(id);
            if (htmlfield == null)
            {
                return HttpNotFound();
            }

            if (htmlfield.PagesFields.Count > 0)
            {
                ModelState.AddModelError("", "This field is used by one or more pages. It must be removed from all linked pages before deleting");
            }
            return View(htmlfield);
        }

        //
        // POST: /HTMLField/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            HTMLField htmlfield = db.HTMLFields.Find(id);
            db.HTMLFields.Remove(htmlfield);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}